﻿
/*
'===============================================================================
'  MP.LayerHelper.MP_OBS.BL.HrBranchManager
'===============================================================================
*/

using System;
using System.Configuration;
using System.Data;
using System.Data.SqlClient;
using MP.LayerHelper.MP_OBS.BLL;
using MP.LayerHelper.MP_OBS.DAL;
using MP.LayerHelper.MP_OBS.DAL.EntityClasses;
using MP.LayerHelper.MP_OBS.DAL.FactoryClasses;
using MP.LayerHelper.MP_OBS.DAL.HelperClasses;
using SD.LLBLGen.Pro.ORMSupportClasses;
using MP.Core.Library;

namespace MP.LayerHelper.MP_OBS.BLL
{
	public class HrBranchManager : HrBranchManagerBase
	{
		/// <summary>
		/// Purpose: Class constructor.
		/// </summary>
		public HrBranchManager()
		{
			// Nothing for now.
		}
		
		/// <summary>
		/// Method to create an instance of HrBranchManager
		/// </summary>
		/// <returns>An instant of HrBranchManager class</returns>
		public static HrBranchManager CreateInstant()
		{
			return new HrBranchManager();
		}

        /// <summary>
        /// Lấy đối tượng chi nhánh theo mã ID
        /// </summary>
        /// <param name="Id"></param>
        /// <returns></returns>
        public HrBranchEntity GetById(Guid Id)
        {
            EntityCollection<HrBranchEntity> items = SelectByIdLST(Id);
            if (items != null && items.Count > 0)
                return items[0];
            return null;
        }

        /// <summary>
        /// Lấy đối tượng chi nhánh theo mã code
        /// </summary>
        /// <param name="BranhCode"></param>
        /// <returns></returns>
        public HrBranchEntity GetByBranchCode(string BranhCode)
        {
            EntityCollection<HrBranchEntity> items = SelectByBranhCodeLST(BranhCode);
            if (items != null && items.Count > 0)
                return items[0];
            return null;
        }

        /// <summary>
        /// Kiểm tra chi nhánh tồn tại chưa theo mã chi nhánh
        /// </summary>
        /// <param name="BranhCode"></param>
        /// <returns></returns>
        public bool IsExistsByBranchCode(string BranhCode)
        {
            HrBranchEntity branh = GetByBranchCode(BranhCode);
            if (branh != null)
                return true;
            return false;
        }

        /// <summary>
        /// Lấy ra danh sách chi nhánh, sắp xếp theo độ ưu tiên và tên chi nhánh
        /// </summary>
        /// <returns></returns>
        public DataSet GetAllBranch()
        {
            string strSQL = "Select * " +
                            "From hrBranch " +
                            "Where IsDeleted=0 " +
                            "Order By Priority, BranhName";
            return SqlHelper.ExecuteDataSet(SqlHelper.ConnectionStringOBS, CommandType.Text, strSQL);
        }

        public void UpdatePriority(Guid Id, int priority)
        {
            string strSQL = "Update hrBranch " +
                            "Set Priority=@Priority " +
                            "Where Id=@Id";
            SqlParameter[] param = new SqlParameter[2];
            param[0] = new SqlParameter("@Priority", priority);
            param[1] = new SqlParameter("@Id", Id);
            SqlHelper.ExecuteNonQuery(SqlHelper.ConnectionStringOBS, CommandType.Text, strSQL, param);
        }

        public DataTable GetByCurrentUserName(string currentUserName)
        {
            string strSQL = "spHR_BRANCH_GetByCurrentUserName";
            SqlParameter[] param = new SqlParameter[1];
            param[0] = new SqlParameter("@currentUserName", currentUserName);
            return SqlHelper.ExecuteDataTable(SqlHelper.ConnectionStringOBS, CommandType.StoredProcedure, strSQL, param);
        }

        public Guid GetBranchMaster(string currentUserName)
        {
            string strSQL = "spHR_BRANCH_GetBranchMaster";
            SqlParameter[] param = new SqlParameter[1];
            param[0] = new SqlParameter("@currentUserName", currentUserName);
            DataTable tbl = SqlHelper.ExecuteDataTable(SqlHelper.ConnectionStringOBS, CommandType.StoredProcedure, strSQL, param);
            if (tbl.Rows.Count > 0)
            {
                return new Guid(tbl.Rows[0]["ID"].ToString());
            }
            return Guid.Empty;
        }
	}
}
